<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="aliyun,">










<meta name="description" content="Aliyun Log Logback AppenderLogback是由log4j创始人设计的又一个开源日志组件。通过使用Logback，您可以控制日志信息输送的目的地是控制台、文件、GUI 组件、甚至是套接口服务器、NT 的事件记录器、  UNIX Syslog 守护进程等；您也可以控制每一条日志的输出格式；通过定义每一条日志信息的级别，您能够更加细致地控制日志的生成过程。最令人感兴趣的就是，这">
<meta name="keywords" content="aliyun">
<meta property="og:type" content="article">
<meta property="og:title" content="sls阿里云日志服务配置">
<meta property="og:url" content="https://blog.xiezc.xyz/2018/10/29/sls阿里云日志服务配置/index.html">
<meta property="og:site_name" content="xiezc的小站">
<meta property="og:description" content="Aliyun Log Logback AppenderLogback是由log4j创始人设计的又一个开源日志组件。通过使用Logback，您可以控制日志信息输送的目的地是控制台、文件、GUI 组件、甚至是套接口服务器、NT 的事件记录器、  UNIX Syslog 守护进程等；您也可以控制每一条日志的输出格式；通过定义每一条日志信息的级别，您能够更加细致地控制日志的生成过程。最令人感兴趣的就是，这">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-04-04T16:09:10.729Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="sls阿里云日志服务配置">
<meta name="twitter:description" content="Aliyun Log Logback AppenderLogback是由log4j创始人设计的又一个开源日志组件。通过使用Logback，您可以控制日志信息输送的目的地是控制台、文件、GUI 组件、甚至是套接口服务器、NT 的事件记录器、  UNIX Syslog 守护进程等；您也可以控制每一条日志的输出格式；通过定义每一条日志信息的级别，您能够更加细致地控制日志的生成过程。最令人感兴趣的就是，这">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://blog.xiezc.xyz/2018/10/29/sls阿里云日志服务配置/">





  <title>sls阿里云日志服务配置 | xiezc的小站</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8ae02d8cf311eabbae2621ff2d51a260";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">xiezc的小站</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">xiezc的小站</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://blog.xiezc.xyz/2018/10/29/sls阿里云日志服务配置/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="xiezc">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="xiezc的小站">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">sls阿里云日志服务配置</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-29T10:51:27+08:00">
                2018-10-29
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/java/" itemprop="url" rel="index">
                    <span itemprop="name">java</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/10/29/sls阿里云日志服务配置/" class="leancloud_visitors" data-flag-title="sls阿里云日志服务配置">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="Aliyun-Log-Logback-Appender"><a href="#Aliyun-Log-Logback-Appender" class="headerlink" title="Aliyun Log Logback Appender"></a>Aliyun Log Logback Appender</h3><p>Logback是由log4j创始人设计的又一个开源日志组件。通过使用Logback，您可以控制日志信息输送的目的地是控制台、文件、GUI 组件、甚至是套接口服务器、NT 的事件记录器、  UNIX Syslog 守护进程等；您也可以控制每一条日志的输出格式；通过定义每一条日志信息的级别，您能够更加细致地控制日志的生成过程。最令人感兴趣的就是，这些可以通过一个配置文件来灵活地进行配置，而不需要修改应用的代码。</p>
<p>通过Aliyun Log Logback Appender，您可以控制日志的输出目的地为阿里云日志服务，写到日志服务中的日志的样式如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">level: ERROR</span><br><span class="line">location: com.aliyun.openservices.log.logback.example.LogbackAppenderExample.main(LogbackAppenderExample.java:18)</span><br><span class="line">message: error log</span><br><span class="line">throwable: java.lang.RuntimeException: xxx</span><br><span class="line">thread: main</span><br><span class="line">time: 2018-01-02T03:15+0000</span><br><span class="line">log: 2018-01-02 11:15:29,682 ERROR [main] com.aliyun.openservices.log.logback.example.LogbackAppenderExample: error log</span><br><span class="line">__source__: xxx</span><br><span class="line">__topic__: yyy</span><br></pre></td></tr></table></figure></p>
<p>其中：</p>
<ul>
<li>level 日志级别。</li>
<li>location 日志打印语句的代码位置。</li>
<li>message 日志内容。</li>
<li>throwable 日志异常信息（只有记录了异常信息，这个字段才会出现）。</li>
<li>thread 线程名称。</li>
<li>time 日志打印时间（可以通过 timeFormat 或 timeZone 配置 time 字段呈现的格式和时区）。</li>
<li>log 自定义日志格式（只有设置了 encoder，这个字段才会出现）。</li>
<li>__source__ 日志来源，用户可在配置文件中指定。</li>
<li>__topic__ 日志主题，用户可在配置文件中指定。</li>
</ul>
<h3 id="功能优势"><a href="#功能优势" class="headerlink" title="功能优势"></a>功能优势</h3><ul>
<li>日志不落盘：产生数据实时通过网络发给服务端。</li>
<li>无需改造：对已使用logback应用，只需简单配置即可采集。</li>
<li>异步高吞吐：高并发设计，后台异步发送，适合高并发写入。</li>
<li>上下文查询：服务端除了通过关键词检索外，给定日志能够精确还原原始日志文件上下文日志信息。</li>
</ul>
<h3 id="版本支持"><a href="#版本支持" class="headerlink" title="版本支持:"></a>版本支持:</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">logback 1.2.3</span><br><span class="line">log-loghub-producer 0.1.13</span><br><span class="line">protobuf-java 2.5.0</span><br></pre></td></tr></table></figure>
<h3 id="配置步骤"><a href="#配置步骤" class="headerlink" title="配置步骤"></a>配置步骤</h3><ol>
<li><p>maven 工程中引入依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.google.protobuf<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>protobuf-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.5.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.aliyun.openservices<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>aliyun-log-logback-appender<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>0.1.13<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>修改配置文件<br>以xml型配置文件logback.xml为例（不存在则在项目根目录创建），配置Loghub相关的appender与 Logger，例如：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--为了防止进程退出时，内存中的数据丢失，请加上此选项--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">shutdownHook</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.hook.DelayingShutdownHook"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"aliyun"</span> <span class="attr">class</span>=<span class="string">"com.aliyun.openservices.log.logback.LoghubAppender"</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--必选项--&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 账号及网络配置 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">endpoint</span>&gt;</span>your project endpoint<span class="tag">&lt;/<span class="name">endpoint</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">accessKeyId</span>&gt;</span>your accesskey id<span class="tag">&lt;/<span class="name">accessKeyId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">accessKey</span>&gt;</span>your accesskey<span class="tag">&lt;/<span class="name">accessKey</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- sls 项目配置 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">projectName</span>&gt;</span>your project<span class="tag">&lt;/<span class="name">projectName</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">logstore</span>&gt;</span>your logstore<span class="tag">&lt;/<span class="name">logstore</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--必选项 (end)--&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- 可选项 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">topic</span>&gt;</span>your topic<span class="tag">&lt;/<span class="name">topic</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">source</span>&gt;</span>your source<span class="tag">&lt;/<span class="name">source</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">&lt;!-- 可选项 详见 '参数说明'--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">packageTimeoutInMS</span>&gt;</span>3000<span class="tag">&lt;/<span class="name">packageTimeoutInMS</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">logsCountPerPackage</span>&gt;</span>4096<span class="tag">&lt;/<span class="name">logsCountPerPackage</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">logsBytesPerPackage</span>&gt;</span>3145728<span class="tag">&lt;/<span class="name">logsBytesPerPackage</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">memPoolSizeInByte</span>&gt;</span>104857600<span class="tag">&lt;/<span class="name">memPoolSizeInByte</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">retryTimes</span>&gt;</span>3<span class="tag">&lt;/<span class="name">retryTimes</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">maxIOThreadSizeInPool</span>&gt;</span>8<span class="tag">&lt;/<span class="name">maxIOThreadSizeInPool</span>&gt;</span></span><br><span class="line">  </span><br><span class="line">  <span class="comment">&lt;!-- 可选项 通过配置 encoder 的 pattern 自定义 log 的格式 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">encoder</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d %-5level [%thread] %logger&#123;0&#125;: %msg<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line">  </span><br><span class="line">  <span class="comment">&lt;!-- 可选项 设置 time 字段呈现的格式 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">timeFormat</span>&gt;</span>yyyy-MM-dd'T'HH:mmZ<span class="tag">&lt;/<span class="name">timeFormat</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!-- 可选项 设置 time 字段呈现的时区 --&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">timeZone</span>&gt;</span>UTC<span class="tag">&lt;/<span class="name">timeZone</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!-- 可用来获取StatusManager中的状态</span></span><br><span class="line"><span class="comment">&lt;statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/&gt;  --&gt;</span></span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>注意：</p>
<ul>
<li>为了防止进程退出时，LoghubAppender缓存在内存中的少量数据丢失，请记得加上DelayingShutdownHook标签。</li>
<li>LoghubAppender在运行过程中产生的异常会被捕获并放入logback的BasicStatusManager类中，您可以通过配置OnConsoleStatusListener或其他方式查看出错信息。参阅：<a href="https://logback.qos.ch/access.html" target="_blank" rel="noopener">https://logback.qos.ch/access.html</a></li>
</ul>
<h3 id="参数说明"><a href="#参数说明" class="headerlink" title="参数说明"></a>参数说明</h3><p>Aliyun Log Logback Appender 可供配置的属性（参数）如下，其中注释为必选参数的是必须填写的，可选参数在不填写的情况下，使用默认值。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">#日志服务的 project 名，必选参数</span><br><span class="line">projectName = [your project]</span><br><span class="line">#日志服务的 logstore 名，必选参数</span><br><span class="line">logstore = [your logstore]</span><br><span class="line">#日志服务的 HTTP 地址，必选参数</span><br><span class="line">endpoint = [your project endpoint]</span><br><span class="line">#用户身份标识，必选参数</span><br><span class="line">accessKeyId = [your accesskey id]</span><br><span class="line">accessKey = [your accesskey]</span><br><span class="line"></span><br><span class="line">#被缓存起来的日志的发送超时时间，如果缓存超时，则会被立即发送，单位是毫秒，默认值为3000，最小值为10，可选参数</span><br><span class="line">packageTimeoutInMS = 3000</span><br><span class="line">#每个缓存的日志包中包含日志数量的最大值，不能超过 4096，可选参数</span><br><span class="line">logsCountPerPackage = 4096</span><br><span class="line">#每个缓存的日志包的大小的上限，不能超过 3MB，单位是字节，可选参数</span><br><span class="line">logsBytesPerPackage = 3145728</span><br><span class="line">#Appender 实例可以使用的内存的上限，单位是字节，默认是 100MB，可选参数</span><br><span class="line">memPoolSizeInByte = 1048576000</span><br><span class="line">#指定I/O线程池最大线程数量，主要用于发送数据到日志服务，默认是8，可选参数</span><br><span class="line">maxIOThreadSizeInPool = 8</span><br><span class="line">#指定发送失败时重试的次数，如果超过该值，会把失败信息记录到logback的StatusManager里，默认是3，可选参数</span><br><span class="line">retryTimes = 3</span><br><span class="line"></span><br><span class="line">#指定日志主题，默认为 &quot;&quot;，可选参数</span><br><span class="line">topic = [your topic]</span><br><span class="line"></span><br><span class="line">#指的日志来源，默认为应用程序所在宿主机的 IP，可选参数</span><br><span class="line">source = [your source]</span><br><span class="line"></span><br><span class="line">#输出到日志服务的时间的格式，默认是 yyyy-MM-dd&apos;T&apos;HH:mmZ，可选参数</span><br><span class="line">timeFormat = yyyy-MM-dd&apos;T&apos;HH:mmZ</span><br><span class="line"></span><br><span class="line">#输出到日志服务的时间的时区，默认是 UTC，可选参数（如果希望 time 字段的时区为东八区，可将该值设定为 Asia/Shanghai）</span><br><span class="line">timeZone = UTC</span><br><span class="line">参阅：https://github.com/aliyun/aliyun-log-producer-java</span><br></pre></td></tr></table></figure></p>
<h3 id="使用实例"><a href="#使用实例" class="headerlink" title="使用实例"></a>使用实例</h3><p>项目中提供了一个名为com.aliyun.openservices.log.logback.LogbackAppenderExample的实例，它会加载resources目录下的logback.xml文件进行logback配置。</p>
<h4 id="logback-xml样例说明"><a href="#logback-xml样例说明" class="headerlink" title="logback.xml样例说明"></a>logback.xml样例说明</h4><ul>
<li>配置了三个appender：loghubAppender1、loghubAppender2、STDOUT。</li>
<li>loghubAppender1：将日志输出到project=test-proj，logstore=store1。输出WARN、ERROR级别的日志。</li>
<li>loghubAppender2：将日志输出到project=test-proj，logstore=store2。只输出INFO级别的日志。</li>
<li>STDOUT：将日志输出到控制台。由于没有对日志级别进行过滤，会输出root中配置的日志级及以上的所有日志。</li>
</ul>
<p>LogbackAppenderExample.java:<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.aliyun.openservices.log.logback.example;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> org.slf4j.Logger;</span><br><span class="line"><span class="keyword">import</span> org.slf4j.LoggerFactory;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Created by brucewu on 2018/1/8.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LogbackAppenderExample</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger LOGGER = LoggerFactory.getLogger(LogbackAppenderExample.class);</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        LOGGER.trace(<span class="string">"trace log"</span>);</span><br><span class="line">        LOGGER.debug(<span class="string">"debug log"</span>);</span><br><span class="line">        LOGGER.info(<span class="string">"info log"</span>);</span><br><span class="line">        LOGGER.warn(<span class="string">"warn log"</span>);</span><br><span class="line">        LOGGER.error(<span class="string">"error log"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>logback-example.xml:<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--为了防止进程退出时，内存中的数据丢失，请加上此选项--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">shutdownHook</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.hook.DelayingShutdownHook"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"loghubAppender1"</span> <span class="attr">class</span>=<span class="string">"com.aliyun.openservices.log.logback.LoghubAppender"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--必选项--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 账号及网络配置 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">endpoint</span>&gt;</span><span class="tag">&lt;/<span class="name">endpoint</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">accessKeyId</span>&gt;</span><span class="tag">&lt;/<span class="name">accessKeyId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">accessKey</span>&gt;</span><span class="tag">&lt;/<span class="name">accessKey</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- sls 项目配置 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">projectName</span>&gt;</span>test-proj<span class="tag">&lt;/<span class="name">projectName</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">logstore</span>&gt;</span>store1<span class="tag">&lt;/<span class="name">logstore</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--必选项 (end)--&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 可选项 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">topic</span>&gt;</span>topic1<span class="tag">&lt;/<span class="name">topic</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">source</span>&gt;</span>source1<span class="tag">&lt;/<span class="name">source</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 可选项 详见 '参数说明'--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">packageTimeoutInMS</span>&gt;</span>3000<span class="tag">&lt;/<span class="name">packageTimeoutInMS</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">logsCountPerPackage</span>&gt;</span>4096<span class="tag">&lt;/<span class="name">logsCountPerPackage</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">logsBytesPerPackage</span>&gt;</span>3145728<span class="tag">&lt;/<span class="name">logsBytesPerPackage</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">memPoolSizeInByte</span>&gt;</span>104857600<span class="tag">&lt;/<span class="name">memPoolSizeInByte</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">retryTimes</span>&gt;</span>3<span class="tag">&lt;/<span class="name">retryTimes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">maxIOThreadSizeInPool</span>&gt;</span>8<span class="tag">&lt;/<span class="name">maxIOThreadSizeInPool</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.classic.filter.ThresholdFilter"</span>&gt;</span><span class="comment">&lt;!-- 打印WARN,ERROR级别的日志 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">level</span>&gt;</span>WARN<span class="tag">&lt;/<span class="name">level</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">filter</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"loghubAppender2"</span> <span class="attr">class</span>=<span class="string">"com.aliyun.openservices.log.logback.LoghubAppender"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--必选项--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 账号及网络配置 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">endpoint</span>&gt;</span><span class="tag">&lt;/<span class="name">endpoint</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">accessKeyId</span>&gt;</span><span class="tag">&lt;/<span class="name">accessKeyId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">accessKey</span>&gt;</span><span class="tag">&lt;/<span class="name">accessKey</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- sls 项目配置 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">projectName</span>&gt;</span>test-proj<span class="tag">&lt;/<span class="name">projectName</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">logstore</span>&gt;</span>store2<span class="tag">&lt;/<span class="name">logstore</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--必选项 (end)--&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 可选项 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">topic</span>&gt;</span>topic2<span class="tag">&lt;/<span class="name">topic</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">source</span>&gt;</span>source2<span class="tag">&lt;/<span class="name">source</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 可选项 详见 '参数说明'--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">packageTimeoutInMS</span>&gt;</span>3000<span class="tag">&lt;/<span class="name">packageTimeoutInMS</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">logsCountPerPackage</span>&gt;</span>4096<span class="tag">&lt;/<span class="name">logsCountPerPackage</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">logsBytesPerPackage</span>&gt;</span>3145728<span class="tag">&lt;/<span class="name">logsBytesPerPackage</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">memPoolSizeInByte</span>&gt;</span>104857600<span class="tag">&lt;/<span class="name">memPoolSizeInByte</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">retryTimes</span>&gt;</span>3<span class="tag">&lt;/<span class="name">retryTimes</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">maxIOThreadSizeInPool</span>&gt;</span>8<span class="tag">&lt;/<span class="name">maxIOThreadSizeInPool</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 可选项 通过配置 encoder 的 pattern 自定义 log 的格式 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d %-5level [%thread] %logger&#123;0&#125;: %msg<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 可选项 设置时间格式 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">timeFormat</span>&gt;</span>yyyy-MM-dd'T'HH:mmZ<span class="tag">&lt;/<span class="name">timeFormat</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 可选项 设置时区 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">timeZone</span>&gt;</span>Asia/Shanghai<span class="tag">&lt;/<span class="name">timeZone</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="tag">&lt;<span class="name">filter</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.classic.filter.LevelFilter"</span>&gt;</span><span class="comment">&lt;!-- 只打印INFO级别的日志 --&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">level</span>&gt;</span>INFO<span class="tag">&lt;/<span class="name">level</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">onMatch</span>&gt;</span>ACCEPT<span class="tag">&lt;/<span class="name">onMatch</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">onMismatch</span>&gt;</span>DENY<span class="tag">&lt;/<span class="name">onMismatch</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">filter</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">appender</span> <span class="attr">name</span>=<span class="string">"STDOUT"</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.ConsoleAppender"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder</span></span><br><span class="line"><span class="comment">            by default --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">encoder</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d&#123;HH:mm:ss.SSS&#125; [%thread] %-5level %logger - %msg<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">appender</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 可用来获取StatusManager中的状态 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">statusListener</span> <span class="attr">class</span>=<span class="string">"ch.qos.logback.core.status.OnConsoleStatusListener"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">root</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">level</span> <span class="attr">value</span>=<span class="string">"DEBUG"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"loghubAppender1"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"loghubAppender2"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">appender-ref</span> <span class="attr">ref</span>=<span class="string">"STDOUT"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">root</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<h3 id="错误诊断"><a href="#错误诊断" class="headerlink" title="错误诊断"></a>错误诊断</h3><p>如果您发现数据没有写入日志服务，可通过如下步骤进行错误诊断。</p>
<ul>
<li>检查配置文件 logback.xml 是否限定了 appender 只输出特定级别的日志。比如，是否设置了 root 或 logger 的 level 属性，是否在 appender 中设置了 filter。</li>
<li>检查您项目中引入的 protobuf-java，aliyun-log-logback-appender 这两个 jar 包的版本是否和文档中maven 工程中引入依赖部分列出的 jar 包版本一致。</li>
<li>通过观察控制台的输出来诊断您的问题。Aliyun Log Logback Appender 会将 appender 运行过程中产生的异常写入 ch.qos.logback.core.BasicStatusManager 中。您可以通过配置 statusListener 来获取 BasicStatusManager 中的数据。例如，<statuslistener class="ch.qos.logback.core.status.OnConsoleStatusListener"> 会将 BasicStatusManager 中的数据输出到控制台。</statuslistener></li>
<li>请检查您的 logback.xml 中是否包含选项 <shutdownhook class="ch.qos.logback.core.hook.DelayingShutdownHook">。数据会定期异步地发往服务端，加上此选项可以保证您的程序在正常退出时，内存中缓存的数据不丢失。</shutdownhook></li>
</ul>
<h3 id="常见问题"><a href="#常见问题" class="headerlink" title="常见问题"></a>常见问题</h3><p>Q：是否支持自定义 log 格式？</p>
<p>A：在 0.1.12 及以上版本新增了 log 字段。您可以通过在 encoder 中设置 pattern 来自定义 log 格式，例如：<br><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">encoder</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">pattern</span>&gt;</span>%d %-5level [%thread] %logger&#123;0&#125;: %msg<span class="tag">&lt;/<span class="name">pattern</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">encoder</span>&gt;</span></span><br></pre></td></tr></table></figure></p>
<p>log 输出样例：<br>        <code>log:  2018-07-15 21:12:29,682 INFO [main] TestAppender: info message.</code></p>
<p>Q：日志中为何没有 time 字段？</p>
<p>A：0.1.6 以及之前的版本的 LogItem 没有包含 time 字段，请升级至最新版本。</p>
<p>Q：用户可以自定义 source 字段的取值吗？</p>
<p>A：0.1.8 以及之前的版本不支持，在这些版本中 source 字段会被设置成应用程序所在宿主机的 IP。在最新的版本中，您可以参考上面的配置文件指定 source 的取值。</p>
<p>Q: 如何采集宿主机 IP？</p>
<p>A: 不要在 logback.xml 中设置 source 字段的值，这种情况下 source 字段会被设置成应用程序所在宿主机的 IP。</p>
<p>Q：在网络发生异常的情况下，aliyun-log-logback-appender 会如何处理待发送的日志？</p>
<p>A：aliyun-log-logback-appender 底层使用 aliyun-log-producer-java 发送数据。producer 会根据您在配置文件中设置的 retryTimes 进行重试，如果超过 retryTimes 次数据仍没有发送成功，会将错误信息输出，并丢弃该条日志。关于如何查看错误输出，可以参考错误诊断部分。</p>
<p>Q：如何关闭某些类输出的日志？</p>
<p>A：通过在 logback.xml 文件中添加 <logger name="packname" level="OFF"> 可屏蔽相应包下日志的输出。 例如，当您在 logback.xml 文件中添加如下内容会屏蔽 package 名为 com.aliyun.openservices.log.producer.inner 下所有类的日志输出。</logger></p>
<p><logger name="com.aliyun.openservices.log.producer.inner" level="OFF"><br>Q：应用初始化时出现这样的信息 A number (N) of logging calls during the initialization phase have been intercepted and are now being replayed. These are subject to the filtering rules of the underlying logging system.？</logger></p>
<p>A：该信息只会在日志系统初始化阶段产生，并不影响后续日志记录的功能。</p>
<p>当应用首次调用LoggerFactory.getLogger()方法时，日志系统进入初始化流程。初始化流程还未结束，再次调用LoggerFactory.getLogger()方法便会出现上述信息。这时，slf4j 会创建替代记录器（substitute loggers）并返回。在完成初始化后，替代记录器（substitute loggers）会将日志记录请求委托给合适的 logger。</p>
<p>aliyun-log-logback-appender 的依赖库 aliyun-log-producer-java 也会使用 slf4j 记录日志，所以会出现上述信息。</p>
<p>参阅：<a href="https://www.slf4j.org/codes.html#replay" target="_blank" rel="noopener">https://www.slf4j.org/codes.html#replay</a></p>
<p>Q：如果想设置 time 字段的时区为东八区或其他时区，该如何指定 timeZone 的取值？</p>
<p>A：当您将 timeZone 指定为 Asia/Shanghai 时，time 字段的时区将为东八区。timeZone 字段可能的取值请参考 java-util-timezone。</p>
<p>贡献者<br>@lionbule @zzboy 对项目作了很大贡献。</p>
<p>感谢 @lionbule @zzboy 的杰出工作。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/aliyun/" rel="tag"># aliyun</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/29/spring_mybatis/Spring Boot 打包分离配置文件/" rel="next" title="Spring Boot 打包分离配置文件">
                <i class="fa fa-chevron-left"></i> Spring Boot 打包分离配置文件
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/29/前端/树莓派安装中文环境/" rel="prev" title="树莓派安装中文环境">
                树莓派安装中文环境 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">xiezc</p>
              <p class="site-description motion-element" itemprop="description">主要一些技术文章, 有些文章是转载的</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">72</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">20</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Aliyun-Log-Logback-Appender"><span class="nav-number">1.</span> <span class="nav-text">Aliyun Log Logback Appender</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#功能优势"><span class="nav-number">2.</span> <span class="nav-text">功能优势</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#版本支持"><span class="nav-number">3.</span> <span class="nav-text">版本支持:</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#配置步骤"><span class="nav-number">4.</span> <span class="nav-text">配置步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参数说明"><span class="nav-number">5.</span> <span class="nav-text">参数说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用实例"><span class="nav-number">6.</span> <span class="nav-text">使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#logback-xml样例说明"><span class="nav-number">6.1.</span> <span class="nav-text">logback.xml样例说明</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#错误诊断"><span class="nav-number">7.</span> <span class="nav-text">错误诊断</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#常见问题"><span class="nav-number">8.</span> <span class="nav-text">常见问题</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xiezc</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("eRAHvO0xVYwckgdXIVpfugRG-gzGzoHsz", "57lKp708hde92YeG1xX9MQj1");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  

  

  

</body>
</html>
